<style lang="less" scoped>
    .rule-list {
        height: 300px;
        border: 1px solid #dddee1;
        border-radius: 5px;
        padding: 10px;
        overflow: auto;
    }
</style>
<template>
    <div>
        <Row>
            <Col span="24">
                <Card style="margin-bottom: 10px">
                    <Form inline>
                        <FormItem style="margin-bottom: 0">
                            <Select v-model="searchConf.status" clearable placeholder='请选择状态' style="width:100px">
                                <Option :value="1">启用</Option>
                                <Option :value="0">禁用</Option>
                            </Select>
                        </FormItem>
                        <FormItem style="margin-bottom: 0">
                            <Input v-model="searchConf.keywords" placeholder="请输入组名称"></Input>
                        </FormItem>
                        <FormItem style="margin-bottom: 0">
                            <Button type="primary" @click="search">搜索/刷新</Button>
                        </FormItem>
                    </Form>
                </Card>
            </Col>
        </Row>
        <Row>
            <Col span="24">
                <Card>
                    <p slot="title" style="height: 32px">
                        <Button type="primary" @click="alertAdd" icon="md-add">新增</Button>
                    </p>
                    <div>
                        <Table :loading="tabLoading" :columns="columnsList" :data="tableData" border disabled-hover>
                            <!--状态-->
                            <template slot-scope="{ row, index }" slot="status">
                                <i-switch v-model="row.status" :true-value="1" :false-value="0" @on-change="statusSwitch(row)"/>
                            </template>
                            <template slot-scope="{ row, index }" slot="memberButton">
                                <!--组成员-->
                                <Button type="primary" style="margin-right: 5px"
                                        @click="()=>{memberSetting.show = true;memberShow.gid = row.id;getMemberList()}">
                                    组成员
                                </Button>
                            </template>
                            <template slot-scope="{ row, index }" slot="option">
                                <!--编辑-->
                                <Button type="primary" style="margin-right: 5px" @click="editShow(row,index)">编辑
                                </Button>
                                <!--删除-->
                                <Poptip word-wrap confirm transfer title="您确定要删除这条数据吗?" @on-ok="Del(row,index)">
                                    <Button type="error" :loading="row.loading">
                                        <span v-if="!row.loading">删除</span>
                                        <span v-else>删除</span>
                                    </Button>
                                </Poptip>
                            </template>
                        </Table>
                    </div>
                    <div class="margin-top-15" style="text-align: center">
                        <Page :total="tableShow.listCount" :current="tableShow.currentPage"
                              :page-size="tableShow.pageSize" @on-change="changePage"
                              @on-page-size-change="changeSize" show-elevator show-sizer show-total></Page>
                    </div>
                </Card>
            </Col>
        </Row>
        <Modal v-model="modalSetting.show" width="668" :styles="{top: '30px'}" @on-visible-change="doCancel">
            <p slot="header" style="color:#2d8cf0">
                <Icon type="md-alert"></Icon>
                <span>{{formItem.id ? '编辑' : '新增'}}权限组</span>
            </p>
            <Form ref="myForm" :rules="ruleValidate" :model="formItem" :label-width="80">
                <FormItem label="组名称" prop="name">
                    <Input v-model="formItem.name" placeholder="请输入权限组名称"></Input>
                </FormItem>
                <FormItem label="组描述" prop="description">
                    <Input type="textarea" v-model="formItem.description" placeholder="请输入权限组描述"></Input>
                </FormItem>
                <FormItem label="组授权" prop="rules">
                    <div class="rule-list">
                        <Tree ref="formTree" :data="ruleList" show-checkbox multiple></Tree>
                    </div>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancel" style="margin-right: 8px">取消</Button>
                <Button type="primary" @click="submit" :loading="modalSetting.loading">确定</Button>
            </div>
        </Modal>
        <Modal v-model="memberSetting.show" width="998" :styles="{top: '30px'}">
            <p slot="header" style="color:#2d8cf0">
                <Icon type="information-circled"></Icon>
                <span>组成员列表</span>
            </p>
            <div>
                <Table :loading="memberTabLoading" :columns="memberColumns" :data="memberData" border disabled-hover>
                    <template slot-scope="{ row, index }" slot="memberDelOption">
                        <!--删除-->
                        <Poptip word-wrap confirm transfer title="您确定要删除这条数据吗?" @on-ok="memberDel(row,index)">
                            <Button type="error" :loading="row.loading">
                                <span v-if="!row.loading">删除</span>
                                <span v-else>删除</span>
                            </Button>
                        </Poptip>
                    </template>
                </Table>
            </div>
            <div class="margin-top-15" style="text-align: center">
                <Page :total="memberShow.listCount" :current="memberShow.currentPage"
                      :page-size="memberShow.pageSize" @on-change="changeMemberPage"
                      @on-page-size-change="changeMemberSize" show-elevator show-sizer show-total></Page>
            </div>
            <p slot="footer"></p>
        </Modal>
    </div>
</template>
<script>
    import {getUsers} from '@/api/user'
    import {getList, add, edit, del, delMember, getRuleList, changeStatus} from '@/api/auth'

    export default {
        name: 'system_auth',
        data() {
            return {
                ruleList: [],
                columnsList: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 65,
                        align: 'center'
                    },
                    {
                        title: '权限组',
                        align: 'center',
                        key: 'name',
                        width: 200
                    },
                    {
                        title: '描述',
                        align: 'center',
                        key: 'description'
                    },
                    {
                        title: '成员授权',
                        align: 'center',
                        width: 116,
                        slot: 'memberButton'
                    },
                    {
                        title: '状态',
                        align: 'center',
                        width: 100,
                        slot: 'status'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        width: 200,
                        slot: 'option'
                    }
                ],
                memberColumns: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 65,
                        align: 'center'
                    },
                    {
                        title: '用户账号',
                        align: 'center',
                        key: 'username'
                    },
                    {
                        title: '用户昵称',
                        align: 'center',
                        key: 'nickname',
                        width: 100
                    },
                    {
                        title: '登录次数',
                        align: 'center',
                        key: 'login_times',
                        width: 100
                    },
                    {
                        title: '最后登录时间',
                        align: 'center',
                        key: 'last_login_time',
                        width: 140
                    },
                    {
                        title: '最后登录IP',
                        align: 'center',
                        key: 'last_login_ip',
                        width: 160
                    },
                    {
                        title: '状态',
                        align: 'center',
                        width: 100,
                        render: (h, params) => {
                            if (params.row.status === 1) {
                                return h('Tag', {
                                    props: {
                                        'color': 'green'
                                    }
                                }, '启用')
                            } else {
                                return h('Tag', {
                                    props: {
                                        'color': 'red'
                                    }
                                }, '不启用')
                            }
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        width: 115,
                        slot: 'memberDelOption'
                    }
                ],
                tableData: [],
                memberData: [],
                tableShow: {
                    currentPage: 1,
                    pageSize: 10,
                    listCount: 0
                },
                memberShow: {
                    currentPage: 1,
                    pageSize: 10,
                    listCount: 0,
                    gid: 0
                },
                searchConf: {
                    keywords: '',
                    status: ''
                },
                modalSetting: {
                    show: false,
                    loading: false,
                    index: 0
                },
                memberSetting: {
                    show: false,
                    loading: false,
                    index: 0
                },
                formItem: {
                    name: '',
                    description: '',
                    rules: [],
                    id: 0
                },
                ruleValidate: {
                    name: [
                        {required: true, message: '组名称不能为空', trigger: 'blur'}
                    ]
                },
                tabLoading: false, // 加载表格数据
                memberTabLoading: false // 加载组员表格数据
            }
        },
        created() {
            this.getList()
        },
        methods: {
            alertAdd() {
                let vm = this
                getRuleList().then(response => {
                    vm.ruleList = response.data.data.list
                })
                this.modalSetting.show = true
            },
            submit() {
                this.formItem.rules = []
                let ruleNodes = this.$refs['formTree'].getCheckedNodes()
                let ruleLength = ruleNodes.length
                if (ruleLength) {
                    for (let i = 0; i <= ruleLength - 1; i++) {
                        this.formItem.rules.push(ruleNodes[i]['key'])
                    }
                }

                let vm = this
                this.$refs['myForm'].validate((valid) => {
                    if (valid) {
                        vm.modalSetting.loading = true
                        if (vm.formItem.id === 0) {
                            add(vm.formItem).then(response => {
                                vm.$Message.success(response.data.msg)
                                vm.getList()
                                vm.cancel()
                            }).catch(() => {
                                vm.modalSetting.loading = false
                            })
                        } else {
                            edit(vm.formItem).then(response => {
                                vm.$Message.success(response.data.msg)
                                vm.getList()
                                vm.cancel()
                            }).catch(() => {
                                vm.modalSetting.loading = false
                            })
                        }
                    }
                })
            },
            cancel() {
                this.modalSetting.show = false
            },
            doCancel(data) {
                if (!data) {
                    this.formItem.id = 0
                    this.$refs['myForm'].resetFields()
                    this.modalSetting.loading = false
                    this.modalSetting.index = 0
                }
            },
            changePage(page) {
                this.tableShow.currentPage = page
                this.getList()
            },
            changeSize(size) {
                this.tableShow.pageSize = size
                this.getList()
            },
            changeMemberPage(page) {
                this.memberShow.currentPage = page
                this.getMemberList()
            },
            changeMemberSize(size) {
                this.memberShow.pageSize = size
                this.getMemberList()
            },
            search() {
                this.tableShow.currentPage = 1
                this.getList()
            },
            getList() {
                let vm = this
                this.tabLoading = true
                let params = {
                    page: vm.tableShow.currentPage,
                    size: vm.tableShow.pageSize,
                    keywords: vm.searchConf.keywords,
                    status: vm.searchConf.status
                }
                getList(params).then(response => {
                    this.tabLoading = false
                    for (let v of response.data.data.list) {
                        v.loading = false
                    }
                    vm.tableData = response.data.data.list
                    vm.tableShow.listCount = response.data.data.count
                }).catch(() => {
                    this.tabLoading = false
                })
            },
            // 获取组员列表
            getMemberList() {
                let vm = this
                this.memberTabLoading = true
                let params = {
                    page: vm.memberShow.currentPage,
                    size: vm.memberShow.pageSize,
                    gid: vm.memberShow.gid
                }
                getUsers(params).then(response => {
                    this.memberTabLoading = false
                    for (let v of response.data.data.list) {
                        v.loading = false
                    }
                    vm.memberData = response.data.data.list
                    vm.memberShow.listCount = response.data.data.count
                }).catch(() => {
                    this.memberTabLoading = false
                })
            },
            // 状态切换
            statusSwitch(row) {
                changeStatus(row.status, row.id).then(response => {
                    this.$Message.success(response.data.msg)
                    this.getList()
                })
            },
            // 显示编辑model
            editShow(row, index) {
                this.formItem.id = row.id
                this.formItem.name = row.name
                this.formItem.description = row.description
                getRuleList({'group_id': row.id}).then(response => {
                    this.ruleList = response.data.data.list
                })
                this.modalSetting.show = true
                this.modalSetting.index = index
            },
            // 删除
            Del(row, index) {
                row.loading = true
                del(row.id).then(response => {
                    row.loading = false
                    this.tableData.splice(index, 1)
                    this.$Message.success(response.data.msg)
                }).catch(() => {
                    row.loading = false
                })
            },
            // 组员删除
            memberDel(row, index) {
                row.loading = true
                delMember({
                    uid: row.id,
                    gid: this.memberShow.gid
                }).then(response => {
                    row.loading = false
                    this.memberData.splice(index, 1)
                    this.$Message.success(response.data.msg)
                }).catch(() => {
                    row.loading = false
                })
            }
        }
    }
</script>

